package com.springboot.others.service.impl;

import com.springboot.others.entity.Customer;
import com.springboot.others.mapper.CustomerMapper;
import com.springboot.others.service.CustomerService;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@CacheConfig(cacheNames = "customers")
public class CustomerServiceImpl implements CustomerService {
    @Resource
    private CustomerMapper customerMapper;

    @Override
    @Cacheable
    public List<Customer> queryAll() {
        return customerMapper.selectAll();
    }

    @Override
    @Cacheable
    public List<Customer> queryByCondition(Customer customer) {
        return customerMapper.selectByCondition(customer);
    }

    @Override
    @Cacheable
    public List<Customer> queryByIds(Integer[] integers) {
        return customerMapper.selectByIds(integers);
    }

    @Override
    @CachePut
    @Transactional(isolation = Isolation.REPEATABLE_READ,propagation = Propagation.REQUIRED)
    public void addOne(Customer customer) {
        customerMapper.insertOne(customer);
    }

    @Override
    @Cacheable
    public void modOne(Customer customer) {
        customerMapper.updateOne(customer);
    }

    @Override
    @CacheEvict
    public void removeOne(Integer id) {
        customerMapper.deleteOne(id);
    }
}
